package io.renren.modules.alarm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.alarm.dao.MessageDao;
import io.renren.modules.alarm.entity.MessageEntity;
import io.renren.modules.alarm.service.MessageService;

@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageDao, MessageEntity> implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MessageEntity> page = this.page(
                new Query<MessageEntity>().getPage(params),
                new QueryWrapper<MessageEntity>().orderByDesc("message_id")
        );

        return new PageUtils(page);
    }

    @Override
    public MessageEntity getCurrent(Long machId) {
        return messageDao.getCurrent(machId);
    }

    @Override
    public Map<String,Object> getEcharts() {
        int[] errors = new int[12];
        int[] normals = new int[12];
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //（1）获取当前时间
        LocalDateTime dateNow = LocalDateTime.now();
        //（2）获取当前时间的前几小时时间
        String[] date = new String[12];
        for (int i = 0; i < 12; i++) {
            date[i] = df.format(dateNow.minusHours(i + 1));
        }


        errors[0] = messageDao.getError(date[0], df.format(dateNow)).size();
        for (int i = 1; i < 12; i++) {
            errors[i] = messageDao.getError(date[i], date[i - 1]).size();
        }

        normals[0] = messageDao.getCount(date[0], df.format(dateNow)).size() - errors[0];
        for (int i = 1; i < 12; i++) {
            errors[i] = messageDao.getCount(date[i], date[i - 1]).size() - errors[i];
        }
        Map map = new HashMap();
        map.put("errors",errors);
        map.put("normals",normals);

        return map;


    }


}